﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Enums;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Applet.RequestDto;
using VisionCloud.Domain.DtoModel.Applet.ResponseDto;
using VisionCloud.Domain.Extension;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsPointCompletedRepository : BaseRepository<MsPointCompleted>, IMsPointCompletedRepository
    {
        public async Task<List<int>> GetCompeltedPointsByFloor(QueryReportConditionDto condition)
        {
            var list = await Db.MsPointCompleteds.Join(Db.CSMSTasks,
                                                    x => x.TaskId,
                                                    y => y.TaskId,
                                                    (x, y) => new { x.PointIndex, y.ProjectId, y.BuildingName, y.FloorName, y.IsDeleteReport, y.MeasureType, y.TaskExecutionType })
                                           .Where(z => !z.IsDeleteReport && z.ProjectId == condition.ProjectId && z.BuildingName == condition.Building && z.FloorName == condition.Floor && z.MeasureType == "0")
                                           .Select(s => s.PointIndex).Distinct()
                                           .ToListAsync();
            return list;
        }

        public async Task<PointReportListDto> GetPointReportList(QueryPointReportDto condition)
        {
            var measureType = condition.MeasureType.ToInt32().ToString();

            IQueryable<PointReport> query = condition.MeasureType switch
            {
                MeasureTypeEnum.实测实量任务 => from pc in Db.MsPointCompleteds
                                          join ct in Db.CSMSTasks on pc.TaskId equals ct.TaskId
                                          join t in Db.MsTasks on pc.TaskId equals t.TaskId
                                          where t.ProjectId == condition.ProjectId
                                          && ct.BuildingName == condition.Building
                                          && ct.FloorName == condition.Floor
                                          //&& t.TaskExecutionType == "1"
                                          && t.MeasureType == measureType
                                          && !t.IsDeleted && !ct.IsDeleteReport
                                          orderby pc.PointIndex ascending
                                          select new PointReport
                                          {
                                              TaskId = t.TaskId,
                                              TaskName = ct.TaskName,
                                              PointIndex = pc.PointIndex,
                                              TaskTypeEnum = t.TaskType,
                                              TaskProperty = t.TaskProperty,
                                              ProjectStandard = ct.ProjectStage,
                                              CompletedDate = pc.ComputeCompletedDate ?? pc.CompletedDate,
                                              ResultDataFilePath = ct.ResultDataFilePath,
                                          },
                MeasureTypeEnum.建模任务 or MeasureTypeEnum.外立面任务 => from ct in Db.CSMSTasks
                                                                 join t in Db.MsTasks on ct.TaskId equals t.TaskId
                                                                 join pt in Db.TaskPointss on ct.TaskId equals pt.TaskId
                                                                 where ct.ProjectId == condition.ProjectId
                                                                 && ct.BuildingName == condition.Building
                                                                 && ct.FloorName == condition.Floor
                                                                 //&& ct.TaskExecutionType == "1"
                                                                 && ct.MeasureType == measureType
                                                                 && pt.status == TaskPoints.PointStatus.测量完成
                                                                 && !t.IsDeleted
                                                                 orderby pt.pintIndex ascending
                                                                 select new PointReport
                                                                 {
                                                                     TaskId = ct.TaskId,
                                                                     TaskName = ct.TaskName,
                                                                     PointIndex = pt.pintIndex,
                                                                     ProjectStandard = ct.ProjectStage,
                                                                     CompletedDate = pt.LastUpdatedDate,
                                                                     TaskTypeEnum = t.TaskType,
                                                                     TaskProperty = t.TaskProperty
                                                                 },
                _ => null
            };

            var today = DateTime.Now;
            DateTime? dateCondition = condition.DateCondition switch
            {
                DateConditionEnum.不限 => null,
                DateConditionEnum.近一周 => today.AddDays(-7),
                DateConditionEnum.近一个月 => today.AddMonths(-1),
                DateConditionEnum.近半年 => today.AddMonths(-6),
                _ => null,
            };
            string[] taskPropertyArr = { TaskPropertyEnum.初测.ToString(), TaskPropertyEnum.复测.ToString() };
            return new PointReportListDto
            {
                PointReports = await query?.WhereIf(condition.PointIndex.HasValue, x => x.PointIndex == condition.PointIndex)
                            .WhereIf(dateCondition.HasValue, x => x.CompletedDate >= dateCondition)
                            .WhereIf(!string.IsNullOrWhiteSpace(condition.ProjectStandard), x => x.ProjectStandard == condition.ProjectStandard)
                            .WhereIf(NotOther(), x => x.TaskProperty == condition.TaskProperty)
                            .WhereIf(IsOther(), x => !taskPropertyArr.Contains(x.TaskProperty))
                            .WhereIf(condition.TaskType.HasValue, x => x.TaskTypeEnum == condition.TaskType)
                            .ToListAsync()
            };

            #region 本地函数

            bool IsOther()
            {
                return condition.MeasureType == MeasureTypeEnum.实测实量任务 && !string.IsNullOrWhiteSpace(condition.TaskProperty) && !taskPropertyArr.Contains(condition.TaskProperty);
            }

            bool NotOther()
            {
                return condition.MeasureType switch
                {
                    MeasureTypeEnum.实测实量任务 => !string.IsNullOrWhiteSpace(condition.TaskProperty) && taskPropertyArr.Contains(condition.TaskProperty),
                    MeasureTypeEnum.建模任务 or MeasureTypeEnum.外立面任务 => !string.IsNullOrWhiteSpace(condition.TaskProperty),
                    _ => false
                };
            }

            #endregion 本地函数
        }
    };
}